from django.http import HttpResponseRedirect
from django.shortcuts import render, HttpResponse

# Create your views here.
from wu.models import *
from wu.forms import *
from dms.settings import UPLOAD_DIR
import random


# 预约成员编辑
def member_register(request):
    if request.method == 'POST':
        form = MemberForm(request.POST)
        if form.is_valid():
            member = form.save()
            request.session['member']=member
            return HttpResponseRedirect('/')
        else:
            return render(request, 'wu/member_edit.html', locals())
    else:
        form = MemberForm()
    return render(request, 'wu/member_register.html', locals())


# 预约人员删除
def member_delete(request, id=0):
    Member.objects.get(id=id).delete()
    return HttpResponseRedirect('/admin/member_list')



# 管理员列表
def member_list(request):
    members = Member.objects.all()
    return render(request, "wu/member_list.html", {'members': members})




# 管理员列表
def manager_list(request):
    managers = Manager.objects.all()
    return render(request, "wu/manager_list.html", {'managers': managers})





# 管理员编辑
def manager_edit(request, id=0):
    id=int(id)
    if id != 0:
        manager = Manager.objects.get(id=id)
    else:
        manager = None
    if request.method == 'POST':
        form = ManagerForm(request.POST, instance=manager)
        if form.is_valid():
            manager = form.save()
            return HttpResponseRedirect('/wu/manager_list')
        else:
            return render(request, 'wu/manager_edit.html', locals())
    else:
        form = ManagerForm(instance=manager)
    return render(request, 'wu/manager_edit.html', locals())


# 管理员删除
def manager_delete(request, id=0):
    Manager.objects.get(id=id).delete()
    return HttpResponseRedirect('/wu/manager_list')





# 管理员编辑
def login_manager(request):
    action="login_manager"
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            mobile = form.cleaned_data['mobile']
            password = form.cleaned_data['password']
            managers=Manager.objects.filter(mobile=mobile,password=password)
            if managers:
                request.session['manager']=managers[0]
                return HttpResponseRedirect('/')
        else:
            return render(request, 'wu/login.html', locals())
    else:
        form = LoginForm()
    return render(request, 'wu/login.html', locals())






# 预约人员管理
def login_member(request):
    action="login_member"
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            manager = form.save()
            return HttpResponseRedirect('/wu/member_index')
        else:
            return render(request, 'wu/login.html', locals())
    else:
        form = LoginForm()
    return render(request, 'wu/login.html', locals())






# 退出
def logout(request):
    request.session["member"]=None
    request.session["manager"]=None

    return HttpResponseRedirect('/')
